class Solution {
    public double myPow(double x, int n) {
        if (n == 0) {
            return 1.0;
        }

        long absN = Math.abs((long) n); // 将指数转换为 long 类型，处理 n = Integer.MIN_VALUE 的情况

        double result = 1.0;
        while (absN > 0) {
            if (absN % 2 == 1) {
                result *= x;
            }
            x *= x;
            absN /= 2;
        }

        return n < 0 ? 1.0 / result : result;
    }
}
